# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=mingw-ldd
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.2.1
pkgrel=1
pkgdesc="Lists dependencies of a PE (exe/dll) file (mingw-w64)"
arch=('any')
url='https://github.com/nurupo/mingw-ldd'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python-pefile")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("${url}/archive/v${pkgver}.tar.gz")
sha256sums=('60d34506d2f345e011b88de172ef312f37ca3ba87f3764f511061b69878ab204')

prepare() {
  rm -rf python-build-${MSYSTEM} || true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py check
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"

  # remove shebang line
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done
}
